#!/usr/bin/env python3

import argparse
import gv.gvsoc_control as gvsoc
import threading
import time
import sys

parser = argparse.ArgumentParser(description='Control GVSOC')

parser.add_argument("--host", dest="host", default="localhost", help="Specify host name")
parser.add_argument("--port", dest="port", default=42951, type=int, help="Specify host port")

args = parser.parse_args()


gv = gvsoc.Proxy(args.host, args.port)

axi = gvsoc.Router(gv)

nb_iter = 100

gv.run()

gv.wait_running()

errors = 0

for i in range(0, nb_iter):
    gv.wait_stop()

    value = axi.mem_read_int(0x1c000000, 4)

    if value != 0x12345678:
        errors += 1

    axi.mem_write_int(0x1c000000, 4, 0x87654321)

    gv.run()

    while True:
        if axi.mem_read_int(0x1c000000, 4) != 0x87654321:
            break

gv.quit(errors)
gv.close()

exit(errors)
 